package com.educhains.web.ctrl.frontend;


import com.educhains.module.demo.vo.Page;
import com.educhains.module.post.service.PostReplyService;
import com.educhains.module.post.service.PostService;
import com.educhains.module.post.vo.*;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.vo.User;
import com.educhains.web.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;


@Controller
public class PostCtrl {


    @Autowired
    private PostService postService;

    @Autowired
    private PostReplyService postReplyService;

    @Autowired
    private UserService userService;


    @RequestMapping("/postMainPage")
    public String postMainPage(HttpServletRequest request){
        return "frontend/post/";
    }

    @RequestMapping("/postReplyPage")
    public String postReplyPage(HttpServletRequest request){
        return "frontend/post/";
    }

    @RequestMapping("/postReleasePage")
    public String postReleasePage( HttpServletRequest request){
        return "frontend/post/newPost";
    }


    @RequestMapping(value = "/addPostReadNum", method = RequestMethod.POST)
    @ResponseBody
    public String addReadNum(HttpServletRequest request) {

        postService.addReadNum(
                // TODO: 2017/9/3  RANDOM ID
                request.getParameter("pid"));

        return "true";

    }

    @RequestMapping("/postList")
    public String postList(HttpServletRequest request){

        String sortOption = request.getParameter("option") == null ? "1" : request.getParameter("option");
        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");

        Page postPage = postService.showPostPage(Integer.valueOf(pageNum), Integer.valueOf(sortOption));
        request.setAttribute("page", postPage);
        request.setAttribute("option", sortOption);

        return "frontend/post/postList";
    }

    @RequestMapping("/postMain")
    public String postMain(HttpServletRequest request) {
        String postId = request.getParameter("pid");


        PostWithContent postWithContent = postService.selectPostDetail(postId);

        List<PostReplyWithContent> postReplyWithContentList = postReplyService.selectPostReplyList(postId);


        request.setAttribute("post", postWithContent);
        request.setAttribute("postReplyList", postReplyWithContentList);
//        return returnBody(true, WebUtils.getRequestPath(request, "/activityMain"));
        return "frontend/post/posts_detail";
    }

    @RequestMapping("/postRelease")
    @ResponseBody
    public String postRelease(HttpServletRequest request){
        String release_content = request.getParameter("release_content");
        String release_postName = request.getParameter("release_postName");
        User sessionUser = SessionUtils.getUser(request);


        Date now = new Date();
        //FIXME time_id replace acticity_id temply ,
        long time_id = System.currentTimeMillis();
        Post post = new Post();
        post.setPostId(String.valueOf(time_id));
        post.setPostSectionId("01");
        post.setPostName(release_postName);
        post.setPostPub(sessionUser.getUserId());
        post.setPostPubName(sessionUser.getUserName());
        post.setPostCreateTime(now);
        post.setPostIsEssence(false);
        post.setPostIsDelete(false);

        PostContent postContent = new PostContent(post.getPostId(), release_content);

        postService.insertSelective(post);
        postService.insertContent(postContent);

        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 20);

        return "true";
    }

    @RequestMapping("/postReply")
    public String postReply(HttpServletRequest request){
        String post_id = request.getParameter("pid");
        String reply_content = request.getParameter("reply_content");
//        String target_reply_id  = request.getParameter("target_reply_id");
        User sessionUser = SessionUtils.getUser(request);


        Date now = new Date();
        long time_id = System.currentTimeMillis();

        PostReply postReply = new PostReply();
        postReply.setReplyId(String.valueOf(time_id));
        postReply.setPostId(post_id);
        postReply.setPostSectionId("01");
        postReply.setPostReplyCreateTime(now);
        postReply.setPostReplyPub(sessionUser.getUserId());
        postReply.setPostReplyPubName(sessionUser.getUserName());
        postReply.setFloorId(String.valueOf(time_id));
        postReply.setPostReplyIsDelete(false);

        PostReplyContent postReplyContent = new PostReplyContent(postReply.getReplyId(), reply_content);

        postReplyService.insertSelective(postReply);
        postReplyService.insertContent(postReplyContent);

        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 5);

        List<PostReplyWithContent> postReplyWithContentList = postReplyService.selectPostReplyList(post_id);

        request.setAttribute("postReplyList", postReplyWithContentList);

        return "frontend/post/postReplyList";
    }


    @RequestMapping(value = "/postInfloor", method = RequestMethod.POST)
    public String postInfloor(HttpServletRequest request){

        String floorId  = request.getParameter("floorId");


        List<Infloor> infloors = postReplyService.selectInfloorByFloorId(floorId);

        request.setAttribute("infloors", infloors);
//        request.setAttribute("post_id", post_id);
        return "frontend/post/postReplyInfloorList";
    }


    @RequestMapping(value = "/postInfloorReply", method = RequestMethod.POST)
    public String postInfloorReply(HttpServletRequest request){

        String infloorContent  = request.getParameter("infloor_reply_content");
        String infloorId  = request.getParameter("infloor");

        User sessionUser = SessionUtils.getUser(request);
        Date now = new Date();

        Infloor infloor = new Infloor();
        infloor.setFloorId(infloorId);
        infloor.setInfloorContent(infloorContent);
        infloor.setInfloorIsDelete(false);
        infloor.setInfloorReplyCreateTime(now);
        infloor.setInfloorReplyPub(sessionUser.getUserId());
        infloor.setInfloorReplyPubName(sessionUser.getUserName());

        postReplyService.insertInfloorSelective(infloor);

        List<Infloor> infloors = postReplyService.selectInfloorByFloorId(infloorId);

        request.setAttribute("infloors", infloors);

        return "frontend/post/postReplyInfloorList";
    }

}
